package com.yt.app.api.action;

import com.yt.app.api.action.base.BaseAction;
import com.yt.app.api.vo.AppTopicVo;
import com.yt.app.api.vo.IndexVo;
import com.yt.app.core.interfaces.AppBannerService;
import com.yt.app.core.interfaces.AppIconService;
import com.yt.app.core.interfaces.AppTopicService;
import com.yt.app.core.model.AppBanner;
import com.yt.app.core.model.AppIcon;
import com.yt.app.core.model.AppTopic;
import com.yt.app.core.utils.Constant;
import com.yt.app.core.utils.HtmlUtil;
import com.yt.core.MsgBean;
import com.yt.core.dao.filter.DynamicSQLFilterImpl;
import com.yt.core.dao.filter.QueryFilter;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import springfox.documentation.annotations.ApiIgnore;

import java.util.ArrayList;
import java.util.List;

@Controller
@Api(tags = "02.首页")
@RequestMapping("/index")
public class IndexController extends BaseAction{
    @Autowired
    private AppBannerService appBannerService;
    @Autowired
    private AppIconService appIconService;
    @Autowired
    private AppTopicService appTopicService;

    @ApiOperation(value="获取显示数据", httpMethod = "GET")
    @ApiResponses(value = {@ApiResponse(code = 200,message = "首页数据", response = IndexVo.class)})
    @RequestMapping("/load")
    public @ResponseBody  Object load(
            @ApiParam("上次最后一个ID,传递接口json中返回的lastId, 只有为0时才加载banner和自定义按钮") @RequestParam(value = "lastTopicId", required = false, defaultValue = "0") Long lastTopicId){
        MsgBean msgBean = new MsgBean();
        IndexVo vo = new IndexVo();
        //只有为0时才获取
        if(lastTopicId == 0) {
            //获取banner数据
            List<AppBanner> bannerList = appBannerService.getValidBannerList(Constant.APP_BANNER_TYPE.Index.value());
            vo.setBanners(bannerList);
            //获取自定按钮
            List<AppIcon> iconList = appIconService.findByType(Constant.APP_ICON_TYPE.Index.value());
            vo.setIcons(iconList);
        }
        //获取话题数据
        QueryFilter filter = new DynamicSQLFilterImpl();
        if(lastTopicId > 0) {
            filter.addFilter("Q_dateline_L_GT", String.valueOf(lastTopicId));
        }
        List<AppTopic> appTopicList = appTopicService.find(filter);
        if(appTopicList != null && !appTopicList.isEmpty()) {
            vo.setLastId(appTopicList.get(appTopicList.size() - 1).getDateline());
            List<AppTopicVo> voList = new ArrayList<AppTopicVo>();
            for(AppTopic topic : appTopicList){
                topic.setContent(HtmlUtil.getShortDesc(topic.getContent(), 100));
                voList.add(new AppTopicVo(topic));
            }
            vo.setTopics(voList);
        }
        msgBean.put("data", vo);
        return msgBean.returnMsg();
    }


    @ApiOperation(value="首页Banner", httpMethod = "GET")
    @ApiResponses(value = {@ApiResponse(code = 200,message = "Banner属性", response = AppBanner.class)})
    @RequestMapping("/banners")
    @ApiIgnore
    public @ResponseBody  Object banners(){
        MsgBean msgBean = new MsgBean();
        List<AppBanner> bannerList = appBannerService.getValidBannerList(Constant.APP_BANNER_TYPE.Index.value());
        msgBean.put("banners", bannerList);

        return msgBean.returnMsg();
    }

    @ApiOperation(value="首页话题流", httpMethod = "GET")
    @ApiResponses(value = {
            @ApiResponse(code = 200,message = "话题", response = AppTopic.class)
    })
    @RequestMapping("/topics")
    public @ResponseBody  Object topics(
            @ApiParam("上次最后一个ID,传递接口json中返回的lastId") @RequestParam(value = "lastId", required = false, defaultValue = "0") Long lastId){
        MsgBean msgBean = new MsgBean();
        QueryFilter filter = new DynamicSQLFilterImpl();
        if(lastId > 0) {
            filter.addFilter("Q_dateline_L_GT", String.valueOf(lastId));
        }
        List<AppTopic> appTopicList = appTopicService.find(filter);
        if(appTopicList != null && !appTopicList.isEmpty()){
            msgBean.put("lastId", appTopicList.get(appTopicList.size()-1).getDateline());
        }else{
            msgBean.put("lastId", -1);
        }
        msgBean.put("topics", appTopicList);

        return msgBean.returnMsg();
    }

}
